using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Application.Excel;
using Admin.NET.Application.Service.Report.Dto;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;

namespace Admin.NET.Application.Service.Report;

/// <summary>
/// 电压统计
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class VoltageReportService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<PowerConsumptionRecord> _rep;

	private readonly SqlSugarRepository<ElectricMeter> _repQuery;

	public VoltageReportService(SqlSugarRepository<PowerConsumptionRecord> rep, SqlSugarRepository<ElectricMeter> repQuery)
	{
		_rep = rep;
		_repQuery = repQuery;
	}

	/// <summary>
	/// 导出电压
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/report/chartsVoltageExecl")]
	public async Task<IActionResult> ExportVoltage([FromBody] ChartsInput input)
	{
		ChartsVoltagerReport list = await GetChartsVoltage(input);
		List<ExportVoltageReport> result = new List<ExportVoltageReport>();
		ElectricMeter meterInfo = await (from u in _repQuery.Context.Queryable<ElectricMeter>()
			where u.Id == input.Id
			select u).FirstAsync();
		for (int i = 0; i < list.Date.Count; i++)
		{
			result.Add(new ExportVoltageReport
			{
				A = list.VoltageA[i],
				B = list.VoltageB[i],
				C = list.VoltageC[i],
				Date = list.Date[i],
				MeterId = meterInfo?.Sn,
				MeterName = meterInfo?.Name
			});
		}
		string str = "电压分析-" + DateTime.Now.ToString("yyyy-MM-dd");
		List<string> column = new List<string>();
		List<string> title = new List<string>();
		string titleEx = "电压分析";
		column.AddRange(new string[6] { "MeterId", "MeterName", "A", "B", "C", "Date" });
		title.AddRange(new string[6] { "表号", "名称", "A相电压", "B相电压", "C相电压", "采集时间" });
		MemoryStream memery = ExportToExcel.ExportToList(result.ToArray(), titleEx, titleEx, column.ToArray(), title.ToArray());
		byte[] aa = memery.ToArray();
		return new FileStreamResult(new MemoryStream(aa), "application/octet-stream")
		{
			FileDownloadName = str + ".xlsx"
		};
	}

	[HttpPost("/report/getChartsVoltage")]
	public async Task<ChartsVoltagerReport> GetChartsVoltage([FromBody] ChartsInput input)
	{
		ChartsVoltagerReport chartsVoltagerReport = new ChartsVoltagerReport();
		var info = await (from u in _rep.Context.Queryable<PowerConsumptionRecord>()
			where u.MeterId == input.Id && u.CreateTime >= input.StartTime && u.CreateTime <= input.EndTime && u.IsHands == false && (u.VoltageA == 0m && u.VoltageB == 0m && u.VoltageC == 0m) == false
			select new { u.Id, u.VoltageA, u.VoltageB, u.VoltageC, u.CreateTime }).ToListAsync();
		chartsVoltagerReport.VoltageA = (from u in info
			orderby u.CreateTime
			select u.VoltageA.ToString() ?? "").ToList();
		chartsVoltagerReport.VoltageB = (from u in info
			orderby u.CreateTime
			select u.VoltageB.ToString() ?? "").ToList();
		chartsVoltagerReport.VoltageC = (from u in info
			orderby u.CreateTime
			select u.VoltageC.ToString() ?? "").ToList();
		chartsVoltagerReport.Date = (from u in info
			orderby u.CreateTime
			select u.CreateTime?.ToString("yyyy-MM-dd HH:mm:ss")).ToList();
		return chartsVoltagerReport;
	}
}
